package org.eoti.ai.genetic.gp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class Criteria<DATA,FITNESS>
{
	protected CopyOnWriteArrayList<Terminal<DATA,FITNESS>> terminalSet;
	protected CopyOnWriteArrayList<Function<DATA,FITNESS>> functionSet;
	protected CopyOnWriteArrayList<Evolution<DATA,FITNESS>> evolutionSet;
	private int populationSize, minProgramSize, maxProgramSize, maxGenerations;
	private Fitness<DATA,FITNESS> fitness;
	private FITNESS fitnessThreshold;
	private boolean allowDuplicates = false;
	private int maxRepeats = -1;
	private boolean debug = false;

	public Criteria()
	{
		terminalSet = new CopyOnWriteArrayList<Terminal<DATA,FITNESS>>();
		functionSet = new CopyOnWriteArrayList<Function<DATA,FITNESS>>();
		evolutionSet = new CopyOnWriteArrayList<Evolution<DATA,FITNESS>>();
	}

	public void addEvolution(Evolution<DATA,FITNESS> evolution)
	{
		evolutionSet.add(evolution);
	}

	public Evolution<DATA,FITNESS> getRandomEvolution()
	{
		ArrayList<Evolution<DATA,FITNESS>> list = new ArrayList<Evolution<DATA,FITNESS>>(evolutionSet);
		Collections.shuffle(list);
		return list.get(RandomNumber.INSTANCE.nextInt(list.size()));
	}

	@SuppressWarnings("unchecked")
	public Terminal<DATA,FITNESS> getRandomTerminal()
	{
		ArrayList<Terminal<DATA,FITNESS>> list = new ArrayList<Terminal<DATA,FITNESS>>(terminalSet);
		Collections.shuffle(list);
		return ( Terminal<DATA,FITNESS> ) list.get( RandomNumber.INSTANCE.nextInt(list.size())).newInstance();
	}

	@SuppressWarnings("unchecked")
	public Function<DATA,FITNESS> getRandomFunction()
	{
		ArrayList<Function<DATA,FITNESS>> list = new ArrayList<Function<DATA,FITNESS>>(functionSet);
		Collections.shuffle(list);
		return ( Function<DATA,FITNESS> ) list.get( RandomNumber.INSTANCE.nextInt(list.size())).newInstance();
	}

	@SuppressWarnings("unchecked")
	public Node<DATA,FITNESS> getRandomNode()
	{
		ArrayList<Node<DATA,FITNESS>> list = new ArrayList<Node<DATA,FITNESS>>();
		list.addAll(functionSet);
		list.addAll(terminalSet);
		Collections.shuffle(list);
		return ( Node<DATA,FITNESS> ) list.get( RandomNumber.INSTANCE.nextInt(list.size())).newInstance();
	}

	public void addTerminal(Terminal<DATA,FITNESS> terminal)
	{
		terminalSet.add(terminal);
	}

	public void addFunction(Function<DATA,FITNESS> function)
	{
		functionSet.add(function);
	}

	public int getPopulationSize(){
		return populationSize;
	}

	public void setPopulationSize( int populationSize ){
		this.populationSize = populationSize;
	}

	public int getMinProgramSize(){
		return minProgramSize;
	}

	public void setMinProgramSize( int minProgramSize ){
		this.minProgramSize = minProgramSize;
	}

	public int getMaxProgramSize(){
		return maxProgramSize;
	}

	public void setMaxProgramSize( int maxProgramSize ){
		this.maxProgramSize = maxProgramSize;
	}

	public int getMaxGenerations(){
		return maxGenerations;
	}

	public void setMaxGenerations( int maxGenerations ){
		this.maxGenerations = maxGenerations;
	}

	public Fitness<DATA, FITNESS> getFitness(){
		return fitness;
	}

	public void setFitness( Fitness<DATA, FITNESS> fitness ){
		this.fitness = fitness;
	}

	public FITNESS getFitnessThreshold(){
		return fitnessThreshold;
	}

	public void setFitnessThreshold( FITNESS fitnessThreshold ){
		this.fitnessThreshold = fitnessThreshold;
	}

	public boolean isAllowDuplicates(){
		return allowDuplicates;
	}

	public void setAllowDuplicates( boolean allowDuplicates ){
		this.allowDuplicates = allowDuplicates;
	}

	public boolean isDebug(){
		return debug;
	}

	public void setDebug( boolean debug ){
		this.debug = debug;
	}

	public int getMaxRepeats(){
		return maxRepeats;
	}

	public void setMaxRepeats( int maxRepeats ){
		this.maxRepeats = maxRepeats;
	}
}
